package com.traitswu.simple.demo.controller;

import com.traitswu.simple.demo.exception.StandardException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;


@ControllerAdvice
@ResponseBody
public class ControllerExceptionHandler {



    @ExceptionHandler(value = StandardException.class)
    public Object handleZeldaAccountException(StandardException exception) {
        HashMap<String, String> map = new HashMap<>();
        map.put("errorCode", exception.getErrorCode());
        map.put("errorMessage", exception.getErrorMessage());
        return map;

    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Object handleDataInvalidateException(MethodArgumentNotValidException exception) {
        HashMap<String, String> map = new HashMap<>();
        map.put("errorCode", "E310");
        map.put("errorMessage", "Invalid field:" + exception.getMessage());
        return map;

    }

    @ExceptionHandler(value = Exception.class)
    public Object otherException(Exception e) {
        HashMap<String, String> map = new HashMap<>();
        map.put("errorCode", "unkown");
        map.put("errorMessage", e.getMessage());
        return map;
    }

}
